<?php
// +----------------------------------------------------------------------
// | QbtCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 千佰特 <http://www.qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_huaxin\controller;

use cmf\controller\PluginAdminBaseController;
use plugins\qbt_huaxin\service\SendSmsService;
use think\Db;
class AdminIndexController extends PluginAdminBaseController

{
    /**
     * 手机短信
     * @adminMenu(
     *     'name'   => '华信短信',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '千佰特手机短信',
     *     'param'  => '',
     * )
     * @adminLog(
     *     'public/plugin/qbt_huaxin/admin_index/index|华信短信页面展示',
     * )
     */
    public function index()
    {
        $huaxin_sms    = cmf_get_option('huaxin_sms');
        $this->assign("info", $huaxin_sms);
        return $this->fetch('/admin_index');
    }

    /**
     * 编辑手机短信
     * @adminMenu(
     *     'name'   => '华信短信',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '千佰特手机短信',
     *     'param'  => '',
     * )
     * @adminLog(
     *     'public/plugin/qbt_huaxin/admin_index/edit|配置华信短信',
     * )
     */
    public function edit(){
        $data = $this->request->param();
        $result = $this->validate($data, "AdminOption");
        if ($result !== true) {
            $this->error($result);
        }
        unset($data['_plugin']);
        unset($data['_controller']);
        unset($data['_action']);
        cmf_set_option('huaxin_sms', $data);
        $this->success("保存成功");
    }
    /**
     * 编辑手机短信
     * @adminMenu(
     *     'name'   => '华信短信',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '千佰特手机短信',
     *     'param'  => '',
     * )
     * @adminLog(
     *     'public/plugin/qbt_huaxin/admin_index/send|测试发送短信',
     * )
     */
    public function send(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $result = $this->validate($data, "AdminSend");
            if ($result !== true) {
                $this->error($result);
            }

            $return = SendSmsService::sendSms($data['code'],$data['mobile']);
            if($return === false){
                $this->error("发送失败！");
            }else{
                $this->success("发送成功！");
            }
        }else{
            return $this->fetch('/admin_send');
        }
    }


}